Here's something I can modify and use:
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
const int MAX_ROWS = 6;
const int MAX_COLS = 6;
class Matrix { // Represents a varying-size matrix that
// can be input from a file
public:
Matrix() {}
Matrix( int, int, int ); // Constructor that initializes matrix
// size and sets all valid elements to
// given initial value
Matrix& operator*=( const Matrix& );
private:
int rows;
int cols;
int mat[MAX_ROWS][MAX_COLS];
friend ostream& operator<< ( ostream&, const Matrix& );
friend istream& operator>> ( istream&, Matrix& );
};
int main()
{
Matrix m1;
Matrix m2;
string file1Name;
string file2Name;
cout << "Name of the file containing first matrix => ";
cin >> file1Name;
ifstream infilOne( file1Name.c_str(), ios::in);
infilOne >> m1;
infilOne.close();
cout << "Name of the file containing second matrix => ";
cin >> file2Name;
ifstream infilTwo(file2Name.c_str(), ios::in);
infilTwo >> m2;
infilTwo.close();
if(!infilOne.fail() && ! infilTwo.fail()){
cout << endl << "Matrix 1" << endl << endl << m1 << endl << endl;
cout << "Matrix 2" << endl << endl << m2 << endl << endl;
m1 *= m2;
cout << "Matrix 1 x Matrix 2 = " << endl << endl << m1 << endl << endl;
}else {
cout << "Error in input" << endl;
}
return 0;
}
//
// Constructor that initializes a Matrix object of size initRows
// x initCols, setting matrix elements to initValue
//
Matrix :: Matrix( int initRows, int initCols, int initValue )
{
rows = initRows;
cols = initCols;
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
mat[i][j] = initValue;
}
//
// Multiplies m1 by m2 if these matrices are conformable.
// Otherwise, displays an error message and sets m1's size
// to 0 x 0
//
Matrix& Matrix :: operator*=(const Matrix& m2 ) // input
{
int val, i, j, k;
Matrix prod;
if (cols != m2.rows) {
cout << "Matrices are not conformable." << endl;
prod.rows = 0;
prod.cols = 0;
} else {
prod.rows = rows;
prod.cols = m2.cols;
for (i = 0; i < prod.rows; ++i)
for (j = 0; j < prod.cols; ++j) {
val = 0;
for (k = 0; k < cols; ++k)
val += mat[i][k] * m2.mat[k][j];
prod.mat[i][j] = val;
}
}
*this = prod;
return *this;
}
//
// Writes to the output stream the contents of matrix m,
// one row at a time
//
ostream& operator<< ( ostream& os, const Matrix& m )
{
for (int i = 0; i < m.rows; ++i) {
for (int j = 0; j < m.cols; ++j)
cout << setw( 5 ) << m.mat[i][j];
cout << endl;
}
return os;
}
//
// Gets the number of rows and columns from the input
// stream. If rows <= MAX_ROWS and cols <= MAX_COLS,
// fills m with data from file; otherwise signals failure
// on stream is. In the event of input failure, sets m size
// to 0 x 0
//
istream& operator>> ( istream& is, Matrix& m )
{
int i, j;
is >> m.rows >> m.cols;
if (is.fail()) {
m.rows = 0;
m.cols = 0;
} else if (m.rows > MAX_ROWS || m.cols > MAX_COLS) {
is.setstate( ios::failbit);
m.rows = 0;
m.cols = 0;
} else {
for (i = 0; i < m.rows; ++i)
for (j = 0; j < m.cols; ++j)
is >> m.mat[i][j];
if (is.fail()) {
m.rows = 0;
m.cols = 0;
}
}
return is;
}
I should be able to use the * function defined here in my ^ function. Not much has to be changed...any ideas?